using System;
using System.Collections.Generic;
using ProtoModels;
using UnityEngine;

public class PlayerSkills : ScriptableObjectSingleton<PlayerSkills>
{
	public enum SkillGroup
	{
		SidespinServe = 0,
		LoopShot = 1
	}

	[Serializable]
	public class SkillItemId
	{
		public string id;

		public SkillItem item
		{
			get
			{
				return ScriptableObjectSingleton<PlayerSkills>.instance.GetItem(id);
			}
		}
	}

	[Serializable]
	public class SkillItem
	{
		[Serializable]
		public class SkillItemSubstage
		{
			public string name;

			public PlayerCareerStageDefinition.GameParameters gameParams = new PlayerCareerStageDefinition.GameParameters();

			public bool useSkill;

			[NonSerialized]
			public int index;

			[NonSerialized]
			public SkillItem skill;

			public bool isLast
			{
				get
				{
					return index >= skill.substages.Count - 1;
				}
			}

			public void Init(int index, SkillItem skill)
			{
				this.index = index;
				this.skill = skill;
			}
		}

		public string nameId;

		public string name;

		public string description;

		public SkillGroup skillGroup;

		public int skillGroupIndex;

		public int row;

		public int column;

		public SkillItemId prev;

		public bool isEnabled = true;

		public List<SkillItemSubstage> substages = new List<SkillItemSubstage>();

		public SkillGroupLevel skillGroupLevel
		{
			get
			{
				return ScriptableObjectSingleton<PlayerSkills>.instance.GetSkillGroupLevel(this);
			}
		}

		public string stageScoreId
		{
			get
			{
				return string.Format("SI-{0}", nameId);
			}
		}

		public bool isActive
		{
			get
			{
				SkillItem item = prev.item;
				if (item == null)
				{
					return true;
				}
				return item.isPassed;
			}
		}

		public bool isPassed
		{
			get
			{
				return score.stars > 0;
			}
		}

		public CareerScoreDAO.StageScore score
		{
			get
			{
				return SingletonInit<PlayerCareer>.instance.GetStageScore(stageScoreId);
			}
		}

		public PlayerCareer.StageProgress progress
		{
			get
			{
				return SingletonInit<PlayerCareer>.instance.GetStageProgress(stageScoreId);
			}
		}

		public int GetStarsCount()
		{
			int num = substages.Count * 3 - 1;
			int num2 = substages.Count * 2 - 1;
			int num3 = 0;
			foreach (SkillItemSubstage substage in substages)
			{
				num3 += progress.GetSubStageProgress(substage.index).playerScore;
			}
			if (num3 >= num)
			{
				return 3;
			}
			if (num3 >= num2)
			{
				return 2;
			}
			return 1;
		}

		public void Init()
		{
			for (int i = 0; i < substages.Count; i++)
			{
				SkillItemSubstage skillItemSubstage = substages[i];
				skillItemSubstage.Init(i, this);
			}
		}

		public int CoinsForContinue(int index)
		{
			int num = 0;
			for (int i = 0; i <= Mathf.Min(index, substages.Count - 1); i++)
			{
				SkillItemSubstage skillItemSubstage = substages[i];
				num += skillItemSubstage.gameParams.coinsRequired;
				EnergyControlConfig instance = EnergyControlConfig.instance;
				num += instance.CoinsForTime(instance.TimeToGainEnergy(skillItemSubstage.gameParams.energyRequired));
			}
			return Mathf.RoundToInt((float)num * 1.65f);
		}

		public void ContinueFromSubstage(int index)
		{
			PlayerCareer.StageProgress stageProgress = progress;
			stageProgress.stage.lastPlayedTime = 0L;
			int wonGames = stageProgress.stage.wonGames;
			stageProgress.stage.wonGames = index;
			stageProgress.stage.playedGames = index;
			for (int i = wonGames; i < index; i++)
			{
				CareerProgressDAO.SubStage subStageProgress = stageProgress.GetSubStageProgress(i);
				subStageProgress.playerScore = Mathf.Max(1, subStageProgress.opponentScore);
			}
			SingletonInit<PlayerCareer>.Instance.Save();
		}
	}

	[Serializable]
	public class SkillGroupLevel
	{
		public SkillGroup skillGroup;

		public int index;

		public LoopParams loopParams;
	}

	public List<SkillItem> skills = new List<SkillItem>();

	public List<SkillGroupLevel> skillLevels = new List<SkillGroupLevel>();

	protected override void Init()
	{
		base.Init();
		foreach (SkillItem skill in skills)
		{
			skill.Init();
		}
	}

	public SkillItem GetItem(string nameId)
	{
		foreach (SkillItem skill in skills)
		{
			if (skill.nameId == nameId)
			{
				return skill;
			}
		}
		return null;
	}

	public float PercentPassed()
	{
		float num = 0f;
		float num2 = 0f;
		foreach (SkillItem skill in skills)
		{
			PlayerCareer.StageProgress progress = skill.progress;
			if (progress.isLost)
			{
				progress.Reset();
			}
			num = ((!skill.isPassed) ? (num + Mathf.InverseLerp(0f, skill.substages.Count - 1, progress.stage.playedGames)) : (num + 1f));
			if (skill.isEnabled)
			{
				num2 += 1f;
			}
		}
		if (num2 == 0f)
		{
			return 1f;
		}
		return num / num2;
	}

	public SkillItem MaxPassedSkillItem(SkillGroup skillGroup)
	{
		SkillItem skillItem = null;
		foreach (SkillItem skill in skills)
		{
			if (skill.skillGroup == skillGroup && (skillItem == null || skillItem.skillGroupIndex <= skill.skillGroupIndex) && skill.isPassed)
			{
				skillItem = skill;
			}
		}
		return skillItem;
	}

	public SkillGroupLevel GetSkillGroupLevel(SkillItem item)
	{
		if (item == null)
		{
			return null;
		}
		SkillGroupLevel skillGroupLevel = null;
		foreach (SkillGroupLevel skillLevel in skillLevels)
		{
			if (skillLevel.skillGroup == item.skillGroup && skillLevel.index <= item.skillGroupIndex && (skillGroupLevel == null || skillGroupLevel.index <= skillLevel.index))
			{
				skillGroupLevel = skillLevel;
			}
		}
		return skillGroupLevel;
	}

	public SkillGroupLevel CurrentSkillGroupLevel(SkillGroup skillGroup)
	{
		SkillItem item = MaxPassedSkillItem(skillGroup);
		return GetSkillGroupLevel(item);
	}
}
